/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.SubjectScheduleDAO;
import com.tces.controller.SubjectStudentDAO;
import com.tces.model.SubjectStudent;
import com.tces.model.User;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author arnolda
 */
public class SubjectStudentAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(SubjectStudentAction.class);
    private List<SubjectStudent> subjectStudentList;
    private User user;
    private String id, oper, subjectOfferingFullName, studentFullName;

    public String execute() throws Exception {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
//            log.info("User role:" + user.getRoleName());
//            if (user.getRoleName().equalsIgnoreCase("Administrator")) {
            subjectStudentList = SubjectStudentDAO.findAll();
//            } else {
//                subjectScheduleList = SubjectScheduleDAO.find(user.getCollegeID());
//            }
            log.info("subjectScheduleList SIZE:" + subjectStudentList.size());

        } catch (Exception ex) {
            log.error(ex);

        }
        return SUCCESS;
    }

    public String edit() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");


            if (oper.equalsIgnoreCase("add")) {

                if (SubjectStudentDAO.add(Integer.parseInt(subjectOfferingFullName), Integer.parseInt(studentFullName)) > 0) {
                    addActionMessage("Successfull insert.");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("edit")) {

                if (SubjectStudentDAO.update(Integer.parseInt(subjectOfferingFullName), Integer.parseInt(studentFullName), Integer.parseInt(id)) > 0) {
                    addActionMessage("Successfull update.");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("del")) {
                SubjectStudent subjectStudent = SubjectStudentDAO.find(Integer.parseInt(id));
                if (SubjectStudentDAO.delete(Integer.parseInt(id)) > 0) {
                    
                    if (SubjectStudentDAO.delete(subjectStudent.getSubjectScheduleID(), subjectStudent.getStudentID()) > 0) {
                        addActionMessage("Successfull deletion of Subject Schedule[ID:" + id + "]");
                        return SUCCESS;
                    } else {
                        addActionError("An error occured. Please contact your System Administrator");
                        return ERROR;
                    }
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else {
                addActionMessage("Invalid operation!");
                return ERROR;
            }
        } catch (Exception ex) {
            addActionError(ex.toString());
            log.error(ex);
            return ERROR;
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public String getStudentFullName() {
        return studentFullName;
    }

    public void setStudentFullName(String studentFullName) {
        this.studentFullName = studentFullName;
    }

    public String getSubjectOfferingFullName() {
        return subjectOfferingFullName;
    }

    public void setSubjectOfferingFullName(String subjectOfferingFullName) {
        this.subjectOfferingFullName = subjectOfferingFullName;
    }

    public List<SubjectStudent> getSubjectStudentList() {
        return subjectStudentList;
    }

    public void setSubjectStudentList(List<SubjectStudent> subjectStudentList) {
        this.subjectStudentList = subjectStudentList;
    }
}
